from flask import Blueprint, request
from libs.provincedata import requestNum, totalSize, totalTime
import json
from datetime import datetime
from flask_cors import CORS

prov_bp = Blueprint("provinceapi", __name__)
cors = CORS(prov_bp)


@prov_bp.route('/area', methods=['GET'])
def get_map_value():
    type = request.args.get('type')
    global start_time, end_time
    start_time = request.args.get('start_time', '').strip('"')
    end_time = request.args.get('end_time', '').strip('"')
    if start_time and end_time:
        start_time = (start_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        end_time = (end_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

    if type == 'requestNum':
        if start_time and end_time:
            data = requestNum(start_time, end_time)
        else:
            data = requestNum()

        with open(r'D:\hm\hm_project\forms\map_response.json', 'r', encoding='utf-8', newline='') as file:
            template = json.load(file)
        myindex = {}
        for feature in template['features']:
            name = feature['properties']['name']
            myindex[name] = feature

        for index, row in data.iterrows():
            for element in myindex:
                if element.startswith(row['remote_ip_province']):
                    myindex[element]['properties']['value'] = row['num']

        return json.dumps(template, indent=4, ensure_ascii=False)

    if type == 'totalSize':
        if start_time and end_time:
            data = totalSize(start_time, end_time)
        else:
            data = totalSize()

        with open(r'D:\hm\hm_project\forms\map_response.json', 'r', encoding='utf-8', newline='') as file:
            template = json.load(file)
        myindex = {}
        for feature in template['features']:
            name = feature['properties']['name']
            myindex[name] = feature

        for index, row in data.iterrows():
            for element in myindex:
                if element.startswith(row['remote_ip_province']):
                    myindex[element]['properties']['value'] = row['object_size']

        return json.dumps(template, indent=4, ensure_ascii=False)

    if type == 'totalTime':
        if start_time and end_time:
            data = totalTime(start_time, end_time)
        else:
            data = totalTime(None, None)

        with open(r'D:\hm\hm_project\forms\map_response.json', 'r', encoding='utf-8', newline='') as file:
            template = json.load(file)
        myindex = {}
        for feature in template['features']:
            name = feature['properties']['name']
            myindex[name] = feature

        for index, row in data.iterrows():
            for element in myindex:
                if element.startswith(row['remote_ip_province']):
                    myindex[element]['properties']['value'] = row['object_time']

        return json.dumps(template, indent=4, ensure_ascii=False)

    else:
        return "Handling logic for other types"
